package com.yzr.common.collection.map;

import java.util.Collection;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

/**
 * @description
 * @author: yx-0173
 * @date: 2021-08-10 11:17
 **/
public class HashMapDemo {
    public static void main(String[] args) {

        Map<Integer, Employee> map = new HashMap<>(16, 0.75f);
        map.put(1, new Employee(1, "tom", 20000));
        map.put(2, new Employee(2, "tony", 30000));
        map.put(3, new Employee(3, "smith", 15000));

        Hashtable<Object, Object> objectObjectHashtable = new Hashtable<>();
//        objectObjectHashtable.put()

        Set<Map.Entry<Integer, Employee>> entries = map.entrySet();
        // 增强for
        for (Map.Entry<Integer, Employee> entry : entries) {
            System.out.println(entry.getClass());
            Employee employee = entry.getValue();
            if (employee.getSal() > 18000) {
                System.out.println(employee);
            }
        }

        System.out.println("=============values============");
        Collection<Employee> values = map.values();
        for (Employee employee : values) {
            if (employee.getSal() > 18000) System.out.println(employee);
        }

    }
}

class Employee {
    private Integer id;

    private String name;

    private Integer sal;

    public Employee(Integer id, String name, Integer sal) {
        this.id = id;
        this.name = name;
        this.sal = sal;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getSal() {
        return sal;
    }

    public void setSal(Integer sal) {
        this.sal = sal;
    }

    @Override
    public String toString() {
        return "Employee{" +
                "id='" + id + '\'' +
                ", name='" + name + '\'' +
                ", sal='" + sal + '\'' +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Employee employee = (Employee) o;
        return Objects.equals(id, employee.id) &&
                Objects.equals(name, employee.name) &&
                Objects.equals(sal, employee.sal);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name);
    }
}
